package com.hainatong.jcmvvmbase.login;

import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.ActivityImgListBinding;
import com.hainatong.jcmvvmbase.login.adapter.ImageBindingAdapter;
import com.hainatong.jcmvvmbase.login.entity.GirlImgBean;
import com.hainatong.jcmvvmbase.login.viewmodel.LoginViewModel;
import com.hainatong.jcmvvmbase.widget.LoadingDialog;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.util.Log;
import android.widget.Toast;
import androidx.recyclerview.widget.LinearLayoutManager;

public class ListActivity extends BaseMvvmActivity<ActivityImgListBinding, LoginViewModel>
{
    private LoadingDialog loadingDialog;
    //private ImageAdapter adapter;
    private ImageBindingAdapter bindingAdapter;

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_img_list;
    }

    @Override
    protected int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initView()
    {
        loadingDialog = new LoadingDialog(this);

        //adapter = new ImageAdapter();
        bindingAdapter = new ImageBindingAdapter();
        bindingAdapter.setOnItemClickListener((adapter, view, position) -> {

            GirlImgBean bean = bindingAdapter.getItem(position);
            Toast.makeText(this,""+bean.getDesc(),Toast.LENGTH_SHORT).show();

        });



        viewDataBinding.listImg.setLayoutManager( new LinearLayoutManager(this));
        viewDataBinding.listImg.setAdapter( bindingAdapter );
        viewDataBinding.listImg.hasFixedSize();
        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader( this ));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter( this ));
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {

            Log.e(TAG , "刷新");

            viewModel.queryGirlImgs();

        } );

        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {

            Log.e(TAG , "加载");

            viewModel.loadMoreGirls();


        } );


    }

    @Override
    protected void initData()
    {
        loadingDialog.show();
        viewModel.queryGirlImgs().observe(this,result ->{

            if( result.isRefresh() )
            {
                if( loadingDialog.isShowing() )
                {
                    loadingDialog.hide();
                }
                if( viewDataBinding.refreshlayout.isRefreshing() )
                {
                    viewDataBinding.refreshlayout.finishRefresh( true );
                }

                if( result.getGirlImgs() != null )
                {
                    bindingAdapter.setList( result.getGirlImgs() );
                }
            }
            else
            {

                //加载更多
                if( viewDataBinding.refreshlayout.isLoading() )
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                    else
                    {
                        viewDataBinding.refreshlayout.finishLoadMore( true );
                    }
                }

                if( result.getGirlImgs() != null )
                {
                    bindingAdapter.addData( result.getGirlImgs() );
                }
            }

            //adapter.setGirlImgs( result );
        });

    }

    @Override
    protected void onRetryBtnClick()
    {
    }
}
